package org.leviatan.dataharbour.gui.listeners;

import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;

import org.leviatan.dataharbour.core.model.PersistenceResource;
import org.leviatan.dataharbour.gui.RuntimeContext;
import org.leviatan.dataharbour.gui.swingmodels.TreeNodeMdlPersistenceResource;

public class PersistenceResourceTreeListener implements TreeSelectionListener {

	private JTree theTree = null;
	
	public PersistenceResourceTreeListener(JTree aTree) {
		this.theTree = aTree;
	}
	
	public void valueChanged(TreeSelectionEvent e) {
		
		DefaultMutableTreeNode selectedNode = 
			(DefaultMutableTreeNode) theTree.getLastSelectedPathComponent();

		if (selectedNode == null) return;
	
		Object nodeModel = selectedNode.getUserObject();
		
		if (selectedNode instanceof TreeNodeMdlPersistenceResource) {
			
			RuntimeContext.persistenceResourceCreatePanel.setVisible(false);
			RuntimeContext.persistenceResourceCreatePanel.setEnabled(false);
			
			RuntimeContext.persistenceResourceDetailView.setVisible(true);
			RuntimeContext.persistenceResourceDetailView.setEnabled(true);
			
			String persResId = ((TreeNodeMdlPersistenceResource) selectedNode).getId();
			
			PersistenceResource persResMdl = 
				RuntimeContext.dataHarbourService.getPersistenceResource(persResId);
			
			RuntimeContext.persistenceResourceDetailView.updateData(persResMdl);
			/*
			if (persResources == null) persResources = new Vector(0);
			RuntimeContext.defsList.setListData(persResources);
			
			Vector persRes = RuntimeContext.dataHarbourService.getDataSourcesPersistenceResources(persResId);
			if (persRes == null) persRes = new Vector(0);
			RuntimeContext.persResList.setListData(persRes);
			*/
		}
			
	}

}
